from django.db import models
from django.contrib.auth.models import User

from problems.models import Problem
from tag.models import Tag
from tag.models import AlgorithmTag
from algorithm.models import TagDependency

class TagChangeRequest(models.Model):
    ADD_OR_DELETE_CHOICES = [
        (0, 'del'),
        (1, 'add'),
    ]
    user = models.ForeignKey(User, on_delete=models.CASCADE)  # 提交请求的用户
    problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
    add_or_delete = models.IntegerField(choices=ADD_OR_DELETE_CHOICES)  # 0为删除，1为添加
    is_approved = models.BooleanField(null=True, blank=True)  # 审核状态：None为未审核，True为通过，False为拒绝
    created_at = models.DateTimeField(auto_now_add=True)
    reviewed_at = models.DateTimeField(null=True, blank=True)
    reviewer = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='reviewed_requests')  # 审核员

    def __str__(self):
        action = "添加" if self.add_or_delete == 1 else "删除"
        return f"{self.user.username} 请求 {action} 标签 {self.tag.tag_name} 到题目 {self.problem.problem_name}"


class TagDependencyChangeRequest(models.Model):
    ADD_OR_DELETE_CHOICES = [
        (0, 'del'),
        (1, 'add'),
    ]

    user = models.ForeignKey(User, on_delete=models.CASCADE)  # 提交请求的用户
    source_tag = models.ForeignKey(AlgorithmTag, related_name='requested_source_dependencies', on_delete=models.CASCADE)
    target_tag = models.ForeignKey(AlgorithmTag, related_name='requested_target_dependencies', on_delete=models.CASCADE)
    weight = models.FloatField(default=1.0)
    add_or_delete = models.IntegerField(choices=ADD_OR_DELETE_CHOICES)  # 0为删除，1为添加
    is_approved = models.BooleanField(null=True, blank=True)  # 审核状态：None为未审核，True为通过，False为拒绝
    created_at = models.DateTimeField(auto_now_add=True)
    reviewed_at = models.DateTimeField(null=True, blank=True)
    reviewer = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='tag_dependency_reviewed_requests')

    def __str__(self):
        action = "添加" if self.add_or_delete == 1 else "删除"
        return f"{self.user.username} 请求 {action} 依赖: {self.source_tag.tag_name} -> {self.target_tag.tag_name}"